package com.example.flash;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
 * http://stackoverflow.com/questions/11915690/android-webview-blank-when-flash-opened-full-screen-ics-4-0
 * http://stackoverflow.com/questions/6780280/weird-webview-behavior-when-playing-flash-game
 * http://stackoverflow.com/questions/6106636/how-to-play-local-swf-files-in-a-webview
 * @param url
 * Success: http://stackoverflow.com/questions/8660866/flash-player-crashes-when-trying-to-enter-fullscreen-mode-android-4-0?lq=1
 * http://stackoverflow.com/questions/5095977/screen-blinking-when-using-a-webview-with-flash?rq=1
 */
public class MainActivity extends Activity {
	private WebView webView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		webView=(WebView)findViewById(R.id.webView1);
		webView.setWebViewClient(new WebViewClient());
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setPluginsEnabled(true);
		webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
		webView.getSettings().setAllowFileAccess(true);
		webView.setScrollContainer(false);
		webView.setWebChromeClient(new FullscreenableChromeClient(this));
		webView.loadUrl(url);
	}
	private String suspendUrl="";
	private String url="file:///android_asset/gameroll.swf";
	@Override
	protected void onPause() {
		Method pause = null;
    	// Pauses the webview.
    	try {
    		pause = WebView.class.getMethod("onPause");
    	} catch (SecurityException e) {
    	} catch (NoSuchMethodException e) {
    	}
    	if (pause != null) {
    		try {
    			pause.invoke(webView);
    		} catch (InvocationTargetException e) {
		} catch (IllegalAccessException e) {
		}
    	} else {
    		// No such method.  Stores the current URL.
    		suspendUrl = webView.getUrl();
    		// And loads a URL without any processing.
    		webView.loadUrl("file:///android_asset/nothing.html");
    	}
		super.onPause();
	}
	@Override
	protected void onResume() {
		Method resume = null;
    	// Resumes the webview.
    	try {
    		resume = WebView.class.getMethod("onResume");
    	} catch (SecurityException e) {
    	} catch (NoSuchMethodException e) {
    	}
    	if (resume != null) {
    		try {
    			resume.invoke(webView);
    		} catch (InvocationTargetException e) {
		} catch (IllegalAccessException e) {
		}
    	} else if (webView != null) {
    		// No such method.  Restores the suspended URL.
    		if (suspendUrl == null) {
    			webView.loadUrl(url);
    		} else {
    			webView.loadUrl(suspendUrl);
    		}
    	}
		super.onResume();
	}

}
